package xdu.lz.stage6_threadpool.chapter4_schedulerExecutorService;

import java.util.concurrent.*;

/**
 * Created at 2019/10/25 0025 下午 12:05
 * Executor
 * -->ExecutorService
 * ---->ScheduledExecutorService
 * ------>ScheduledThreadPoolExecutor extends ThreadPoolExecutor implements ScheduledExecutorService
 * <p>
 * <p>
 *
 * public interface ScheduledExecutorService extends ExecutorService {

        ScheduledFuture<?> schedule(Runnable var1, long var2, TimeUnit var4);

        <V> ScheduledFuture<V> schedule(Callable<V> var1, long var2, TimeUnit var4);

        ScheduledFuture<?> scheduleAtFixedRate(Runnable var1, long var2, long var4, TimeUnit var6);

        ScheduledFuture<?> scheduleWithFixedDelay(Runnable var1, long var2, long var4, TimeUnit var6);
 }
 */
public class SchedulerExecutorService1 {


    public static void main(String[] args) {


    }

    /**
     * {@link ScheduledExecutorService#schedule(Runnable, long, TimeUnit)}
     * {@link ScheduledExecutorService#schedule(Callable, long, TimeUnit)}
     * {@link ScheduledExecutorService#scheduleAtFixedRate(Runnable, long, long, TimeUnit)}
     * {@link ScheduledExecutorService#scheduleWithFixedDelay(Runnable, long, long, TimeUnit)}
     */
    public static void testSubmit() {

        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
        //返回future仅仅是提供取消，是否完成等功能，并无返回结果,Runnable
        ScheduledFuture<?> future = executor.schedule(() -> {
        }, 1, TimeUnit.SECONDS);
        future.cancel(true);
        future.isDone();
        //Callable 有返回结果
        ScheduledFuture<Integer> future1 = executor.schedule(() -> 1, 1, TimeUnit.SECONDS);

        //周期执行API,task first,第一次延迟2s
        executor.scheduleAtFixedRate(() -> {
        }, 2, 1, TimeUnit.SECONDS);


        //当任务执行完毕后，延迟delay，然后再重复执行
        executor.scheduleWithFixedDelay(() -> {
        }, 1, 1, TimeUnit.SECONDS);


    }
}
